package com.neuedu.mvc.service;

import com.neuedu.mvc.dao.DiseaseDao;
import com.neuedu.mvc.entity.Disease;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务逻辑层
 */
public class DiseaseService {

    DiseaseDao diseaseDao = new DiseaseDao();


    /**
     * 根据条件查询列表
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public List<Disease> list(HttpServletRequest request) throws SQLException, ClassNotFoundException {

        String diseaseName = request.getParameter("diseaseName");

        String sql = "select * from disease ";
        Object[] params =  null;
        if(diseaseName!=null && diseaseName.trim().length()>0){
            sql += " where disease_name like ? ";
            params = new Object[]{"%"+diseaseName+"%"};
        }


        List<Disease> diseases = diseaseDao.selectList(sql,params);

        return diseases ;
    }


    /**
     * 带分页的查询
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public Map listPage(HttpServletRequest request) throws SQLException, ClassNotFoundException {

        String diseaseName = request.getParameter("diseaseName");
        String sql = "select * from disease ";
        Object[] params =  null;
        if(diseaseName!=null && diseaseName.trim().length()>0){
            sql += " where disease_name like ? ";
            params = new Object[]{"%"+diseaseName+"%"};
        }

        int current = 1;
        int size = 10;
        try{
            if(request.getParameter("current") != null) {
                current = Integer.parseInt(request.getParameter("current"));
            }
            if(request.getParameter("size") != null) {
                size = Integer.parseInt(request.getParameter("size"));
            }
        }finally{}

        List<Disease> diseases = diseaseDao.selectPage(sql ,current,size,params);
        long total = diseaseDao.count(sql,params);


        Map result = new HashMap();

        result.put("current",current);
        result.put("size",size);
        result.put("total",total);
        result.put("data",diseases);


        return result ;
    }


    /**
     * 根据输入的信息添加一条
     *  `disease_code` varchar(100) NOT NULL COMMENT '疾病助记编码',
     *   `disease_name` varchar(100) DEFAULT NULL COMMENT '疾病名称',
     *   `disease_icd` varchar(100) NOT NULL COMMENT '国际ICD编码',
     *   `cate_id` int(11) DEFAULT NULL COMMENT '主键',
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean save(HttpServletRequest request) throws SQLException, ClassNotFoundException {

        String diseaseCode = request.getParameter("diseaseCode");
        String diseaseName = request.getParameter("diseaseName");
        String diseaseIcd = request.getParameter("diseaseIcd");
        String cateId = request.getParameter("cateId");

        String sql = "insert into disease (disease_code, disease_name,disease_icd,cate_id) values(?,?,?,?) ";

        int count = diseaseDao.insert(sql,diseaseCode,diseaseName,diseaseIcd,cateId);

        return count >0;


    }


    /**
     * 根据主键查询信息
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public Disease queryById(HttpServletRequest request) throws SQLException, ClassNotFoundException {
        String id = request.getParameter("id");

        return diseaseDao.selectOne(Integer.parseInt(id));
    }


    /**
     * 更具主键更新
     * @param request
     * @return
     */
    public boolean update(HttpServletRequest request) throws SQLException, ClassNotFoundException {
        String id = request.getParameter("id");

        String diseaseCode = request.getParameter("diseaseCode");
        String diseaseName = request.getParameter("diseaseName");
        String diseaseIcd = request.getParameter("diseaseIcd");
        String cateId = request.getParameter("cateId");

        String sql = "UPDATE   disease SET  `disease_code` = ?, `disease_name` = ?  ,  `disease_icd` = ?   ,  `cate_id` = ? WHERE `id` =  ?  ";

        int count = diseaseDao.update(sql,diseaseCode,diseaseName,diseaseIcd,cateId,id);

        return count >0;
    }


    /**
     *  根据主键删除一条数据
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean removeById(HttpServletRequest request ) throws SQLException, ClassNotFoundException {

        String id = request.getParameter("id");
        return diseaseDao.deleteById(Integer.parseInt(id)) > 0;

    }
}
